import pandas as pd
from selenium import webdriver
import time
from lxml import etree
from selenium.webdriver.chrome.options import Options
from datetime import datetime, timedelta

chrome_options = Options()
chrome_options.add_argument("--headless")       # define headless
driver = webdriver.Chrome(chrome_options=chrome_options)

show_table = pd.read_csv('user_star.csv')
ID = show_table['用户ID']
ID_list = list(ID)

Id_list = []
for id in ID_list:
    Id_list.append(str(id))
print(Id_list)


# 创建（用户名，发布时间，正文内容，点赞数，评论数，转发数）总列表
user2_list = []
Time_list = []
Text_list = []
DianZan_list = []
ZhuanFa_list = []
PingLun_list = []

for ID in Id_list:
    url = 'https://m.weibo.cn/u/'+str(ID)
    driver.get(url)
    # 使用selenium获取页面，等待10秒，使页面加载完
    time.sleep(5)

    js = "var q=document.documentElement.scrollTop=100000"
    for i in range(2):
        driver.execute_script(js)
        time.sleep(2)

    html = etree.HTML(driver.page_source)

    # 使用selenium库对页面进行解析，提取出相应内容
    Text = driver.find_elements_by_xpath('//*[@id]/div/div/div/div/div/article/div[1]/div[1]')  # 正文
    users = driver.find_elements_by_xpath("//*[@id]/div/div/div/div/div/header/div/div/a/h3")  # 昵称
    # 使用xpath对页面进行解析，提取出相应内容
    Times = html.xpath('//*[@id]/div/div/div/div/div/header/div/div/h4/span[1]')  # 发布时间
    ZhuanFa = html.xpath('//*[@id]/div/div/div/div/div/footer/div[1]/h4/text()')   # 转发数
    PingLun = html.xpath('//*[@id]/div/div/div/div/div/footer/div[2]/h4/text()')   # 评论数
    DianZan = html.xpath('//*[@id]/div/div/div/div/div/footer/div[3]/h4/text()')   # 点赞数

    # 将每一页的列表内容转到总列表中
    for user in users:
        user2_list.append(user.text)
    for Time in Times:
        created_at = Time.text
        if u"刚刚" in created_at:
            created_at = datetime.now().strftime("%Y-%m-%d")
        elif u"分钟" in created_at:
            minute = created_at[:created_at.find(u"分钟")]
            minute = timedelta(minutes=int(minute))
            created_at = (datetime.now() - minute).strftime("%Y-%m-%d")
        elif u"小时" in created_at:
            hour = created_at[:created_at.find(u"小时")]
            hour = timedelta(hours=int(hour))
            created_at = (datetime.now() - hour).strftime("%Y-%m-%d")
        elif u"昨天" in created_at:
            day = timedelta(days=1)
            created_at = (datetime.now() - day).strftime("%Y-%m-%d")
        elif created_at.count('-') == 1:
            year = datetime.now().strftime("%Y")
            created_at = year + "-" + created_at
        Time_list.append(created_at)
    for text in Text:
        Text_list.append(text.text)
        # 规范点赞数，转发数，评论数格式
    for Dianzans in DianZan:
        dianzans = str(Dianzans).replace('万', '0000').replace('万+', '0000').replace('.', '').replace('赞', '0')
        DianZan_list.append(dianzans)
    for Zhuanfas in ZhuanFa:
        zhuanfas = str(Zhuanfas).replace('万', '0000').replace('万+', '0000').replace('.', '').replace('转发', '0')
        ZhuanFa_list.append(zhuanfas)
    for Pingluns in PingLun:
        pingluns = str(Pingluns).replace('万', '0000').replace('万+', '0000').replace('.', '').replace('评论', '0')
        PingLun_list.append(pingluns)

    # 保存为CSV文件
    show_table = {
        '发布人': user2_list,
        '发布时间': Time_list,
        '正文内容': Text_list,
        '点赞数': DianZan_list,
        '评论数': PingLun_list,
        '转发数': ZhuanFa_list
    }
    show_table_df = pd.DataFrame(show_table)
    show_table_df.to_csv('user_write_context.csv', index=False, encoding='UTF-8')

    print("********用户"+str(ID)+"爬取完成*********")



